package com.sqx.modules.stockManage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.stockManage.entity.StockManage;
import com.sqx.modules.stockManage.service.StockManageService;
import com.sqx.modules.stockManage.dao.StockManageDao;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author Admin
 * @description 针对表【stock_manage】的数据库操作Service实现
 * @createDate 2025-03-06 16:26:09
 */
@Service
public class StockManageServiceImpl extends ServiceImpl<StockManageDao, StockManage>
        implements StockManageService {

    @Override
    public Result addStockManage(StockManage stockManage) {
        if (stockManage.getId() != null) {
            if (stockManage.getStock() - stockManage.getOutStock() < 0) {
                return Result.error("出售库存不能大于总库存数量，请重新输入");
            }
            stockManage.setOutTime(new Date());
            return Result.upStatus(baseMapper.updateById(stockManage));
        } else {
            if (stockManage.getStock() - stockManage.getOutStock() < 0) {
                return Result.error("总库存数量小于出售库存数量，请重新输入");
            }
            stockManage.setCreateTime(new Date());
            return Result.upStatus(baseMapper.insert(stockManage));
        }
    }

    @Override
    public Result deleteStockManage(Long id) {
        StockManage stockManage = baseMapper.selectById(id);
        if (stockManage != null) {
            return Result.success().put("data", baseMapper.deleteById(id));
        }
        return Result.error("删除失败");
    }

    @Override
    public Result selectStockManageById(Long id) {
        if (baseMapper.selectById(id) != null) {
            return Result.success().put("data", baseMapper.selectById(id));
        }
        return Result.error("库存商品不存在");
    }

    @Override
    public IPage<StockManage> getStockManageList(Integer page, Integer limit, StockManage stockManage) {
        IPage<StockManage> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
        }
        return baseMapper.selectPageByAllId(pages, stockManage);
    }
}




